package com.apps.ftapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.UserAddressAdapter;
import com.apps.ftapp.model.Uaddress;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

public class UserAddressListActivity extends BaseActivity implements UserAddressAdapter.MyClickListener {

    @BindView(R.id.listView)
    ListView listView;

    private Gson gson = new Gson();


    private int resultCode = 0;

    private UserAddressAdapter userAddressAdapter;
    private List<Uaddress.AddressBean> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_address_list);
        ButterKnife.bind(this);

        setTitle("我的地址");
        qingkong.setVisibility(View.VISIBLE);
        qingkong.setText("新增");
        qingkong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("id", "");
                ActivitySkipUtil.skipAnotherActivityResult(UserAddressListActivity.this, UserAddressAddActivity.class, data, 100);

            }
        });
        initView();
        loadData();

    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("my/address", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    Uaddress result = gson.fromJson(new String(responseBody), Uaddress.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {

                        dataList.addAll(result.getAddress());
                        userAddressAdapter.addRes(result.getAddress());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    private void initView() {

        userAddressAdapter = new UserAddressAdapter(getApplicationContext(), dataList, this);
        listView.setAdapter(userAddressAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent mIntent = new Intent();
                mIntent.putExtra("name", dataList.get(position).getUser_name());
                mIntent.putExtra("mobile", dataList.get(position).getMobile());
                mIntent.putExtra("address", dataList.get(position).getAddress());
                mIntent.putExtra("addressId", dataList.get(position).getAddress_id());
                setResult(resultCode, mIntent);
                finish();
            }
        });

    }

    @Override
    public void clickListener(View v) {

        int position = (int) v.getTag();

        switch (v.getId()) {

            case R.id.address_top:
                Intent mIntent = new Intent();
                mIntent.putExtra("name", dataList.get(position).getUser_name());
                mIntent.putExtra("mobile", dataList.get(position).getMobile());
                mIntent.putExtra("address", dataList.get(position).getAddress());
                mIntent.putExtra("addressId", dataList.get(position).getAddress_id());
                setResult(resultCode, mIntent);
                finish();

                break;

            case R.id.address_edit:

                HashMap<String, Object> data = new HashMap<String, Object>();
//                data.put("id","");
                data.put("name", dataList.get(position).getUser_name());
                data.put("mobile", dataList.get(position).getMobile());
                data.put("address", dataList.get(position).getAddress());
                data.put("province", dataList.get(position).getProvince());
                data.put("citys", dataList.get(position).getCity());
                data.put("district", dataList.get(position).getDistrict());
                data.put("addressId", dataList.get(position).getAddress_id());


                data.put("city", dataList.get(position).getProvince() + " " + dataList.get(position).getCity() + " " + dataList.get(position).getDistrict());
                ActivitySkipUtil.skipAnotherActivityResult(UserAddressListActivity.this, UserAddressAddActivity.class, data, 100);


                break;

            case R.id.address_del:


                dels(dataList.get(position).getAddress_id(), position);
                break;
        }
    }

    private void dels(String id, final int position) {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("address_id", id);
        Async.post("my/address/delete", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    Map jsonBean = gson.fromJson(new String(responseBody), Map.class);
                    L.i("main", jsonBean.toString());
                    if (1 == Tools.isDoubleNull(jsonBean.get("success")) || 1.0 == Tools.isDoubleNull(jsonBean.get("success"))) {
                        dataList.remove(dataList.get(position));
                        userAddressAdapter.notifyDataSetChanged();
//                        finish();
                    } else {
                        Toasty.normal(getApplicationContext(), Tools.isNull(jsonBean.get("error"))).show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 100:
                //
                dataList.clear();
                loadData();
                //
            case 2:
                //来自按钮2的请求，作相应业务处理
        }
    }


}
